package com.calvin.lingting.utils;

import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import com.calvin.lib.json.JSONArray;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lingting.R;
import com.calvin.lingting.config.AppConfig;
import com.calvin.lingting.config.LrtsConfig;
import com.calvin.lingting.config.NTConfig;

import java.io.File;
import java.util.concurrent.Executor;

public class Lrts {

    private Handler mHandler;
    private static Lrts mInstance;
    private Executor mExecutor = Utils.mExecutor;

    private Lrts() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        ImageView view = (ImageView) msg.obj;
                        view.setImageBitmap(BitmapFactory.decodeFile(view.getTag().toString()));
                        break;
                    case 1:
                        NotificationCenter.shared().postNotice(NTConfig.NT_DS_SEARCH, msg.obj);
                        break;
                    case 2:
                        NotificationCenter.shared().postNotice(NTConfig.NT_DS_CHARPTER, msg.obj);
                        break;
                    default:
                        break;
                }
            }
        };
    }

    public static Lrts shared() {
        return mInstance != null ? mInstance : (mInstance = new Lrts());
    }

    public Handler getHandler() {
        return mHandler;
    }

    public void setHandler(Handler mHandler) {
        this.mHandler = mHandler;
    }

    public void search(final String name, final int page) {
        mExecutor.execute(new Runnable() {
            public void run() {
                JSONArray newList = new JSONArray();
                LrtsUtils.seachBook(name, page, newList);
                Message.obtain(mHandler, 1, newList).sendToTarget();
            }
        });
    }

    public void getChapter(final JSONObject bookInfo, final int idx) {
        mExecutor.execute(new Runnable() {
            public void run() {
                JSONObject newBookInfo = new JSONObject();
                LrtsUtils.getBookList(bookInfo.getStringValue(LrtsConfig.K_BOOK_ID), idx, newBookInfo);
                Message.obtain(mHandler, 2, newBookInfo).sendToTarget();
            }
        });
    }

    public void download(final JSONObject book, final int chapter) {
        File file = Environment.getExternalStoragePublicDirectory(String.format("%s/%s", AppConfig.SD_BOOK, book.getStringValue(LrtsConfig.K_NAME)));
        if (!file.exists()) {
            file.mkdirs();
        }
        final File outFile = new File(file.getPath(), String.format("%s.mp3", chapter));
        if (!outFile.exists()) {
            mExecutor.execute(new Runnable() {
                public void run() {
                    Utils.downloadFile(book.getJSONObject(LrtsConfig.K_CHAPTER_LIST).getStringValue(chapter + ""), outFile);
                }
            });
        }
    }

    public String getBookPhoto(String path) {
        File file = Environment.getExternalStoragePublicDirectory(AppConfig.SD_PHOTO);
        return String.format("%s/%s", file.getPath(), path.split("/")[3]);
    }

    public void setBookPhoto(final ImageView view, final String path) {
        final File outFile = new File(getBookPhoto(path));
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }
        if (!outFile.exists()) {
            view.setImageResource(R.drawable.photo);
            view.setTag(outFile.getPath());
            mExecutor.execute(new Runnable() {
                public void run() {
                    Utils.downloadFile(path, outFile);
                    Message.obtain(mHandler, 0, view).sendToTarget();
                }
            });
        } else {
            view.setImageBitmap(BitmapFactory.decodeFile(outFile.getPath()));
        }
    }
}
